package com.is.demo.generator.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.is.demo.generator.entity.User;
import com.is.demo.generator.mapper.UserMapper;
import com.is.demo.generator.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author will
 * @since 2020-07-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> test() {
        return userMapper.test();
    }

    @Override
    public String getName(String id) {
        return userMapper.getName(id);
    }

    @Override
    public List<User> queryByCondition(Map<String, Object> map) {
        Object b = map.get("b");
        Object a = map.get("a");
        Object nameLike = map.get("nameLike");

        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.ge("age", b).and(x -> x.lt("age", a));

        if (ObjectUtil.isNotEmpty(nameLike)) {
            wrapper.and(x -> x.like("name", nameLike));
        }

        wrapper.orderByAsc("age", "name");

        return userMapper.selectList(wrapper);
    }

    @Override
    public Page<User> queryPage(long current, long size){
        Page<User> page = new Page<>(current,size);
        Page<User> res = userMapper.selectPage(page, Wrappers.emptyWrapper());
        return res;
    }
}
